//
//  HWSaveMoneyCardCreateCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/6/29.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit

class HWSaveMoneyCardCreateCell: UITableViewCell {
    var titleImageView:UIImageView!
    var leftBgView:UIView!
    var leftTitleBgView:UIImageView!
    var leftTitleLabel:UILabel!
    var leftSelectImageView:UIImageView!
    var leftContentOne:UILabel!
    var leftContentTwo:UILabel!
    var leftContentThird:UILabel!
    var leftBgButton:UIButton!
    
    var rightBgView:UIView!
    var rightTitleBgView:UIImageView!
    var rightTitleLabel:UILabel!
    var rightSelectImageView:UIImageView!
    var rightContentOne:UILabel!
    var rightContentTwo:UILabel!
    var rightContentThird:UILabel!
    var rightBgButton:UIButton!
    
    var openButton:UIButton!
    var bottomLabel:UILabel!
    
    var model:HWSaveModeyCardModel? {
        didSet {
            leftContentOne.text = "·总计可领取\(model?.zhou_data.zongji ?? "")代金券"
            leftContentTwo.text = "·开通即送\(model?.zhou_data.ktjs ?? "")代金券"
            leftContentThird.text = "·每日再领\(model?.zhou_data.mrzl ?? "")代金券"
            
            rightContentOne.text = "·总计可领取\(model?.yue_data.zongji ?? "")代金券"
            rightContentTwo.text = "·开通即送\(model?.yue_data.ktjs ?? "")代金券"
            rightContentThird.text = "·每日再领\(model?.yue_data.mrzl ?? "")代金券"
            
            bottomLabel.text = "游戏累计实付\(model?.money_limit ?? "")即可开通，当前累计实付\(model?.paymoney ?? "")"
            
            if model?.selectZhou == true {
                leftSelectImageView.isHidden = false
                leftBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0.1)
                
                rightSelectImageView.isHidden = true
                rightBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0)
            } else {
                leftSelectImageView.isHidden = true
                leftBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0)
                
                rightSelectImageView.isHidden = false
                rightBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0.1)
            }
            
            if model?.is_buy == "1" {
                openButton.setTitle("已开通7天周卡", for: .normal)
                openButton.isEnabled = false
//                UIColor.gradientColor(view: openButton, colors: [UIColor.lightGray.cgColor,
//                                                                 UIColor.lightGray.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                openButton.backgroundColor = .lightGray
            } else if model?.is_buy == "2" {
                openButton.setTitle("已开通30天月卡", for: .normal)
                openButton.isEnabled = false
                openButton.backgroundColor = .lightGray
//                UIColor.gradientColor(view: openButton, colors: [UIColor.lightGray.cgColor,
//                                                                 UIColor.lightGray.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
            } else {
                openButton.isEnabled = true
                
                UIColor.gradientColor(view: openButton, colors: [UIColor.colorWithHexString(hexString: "#FDC013").cgColor,
                                                                 UIColor.colorWithHexString(hexString: "#FA8C08").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
                if model?.selectZhou == true {
                    openButton.setTitle("\(model?.zhou_data.jiage ?? "")开通超级省钱卡", for: .normal)
                }else {
                    openButton.setTitle("\(model?.yue_data.jiage ?? "")开通超级省钱卡", for: .normal)
                }
            }
        }
    }
    
    var selectButtonClick:(()->())?
    var payButtonClick:((_:String)->())?
    class func cellWithTableView(tableView:UITableView) -> (HWSaveMoneyCardCreateCell) {
        let reUse:String = "HWSaveMoneyCardCreateCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWSaveMoneyCardCreateCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWSaveMoneyCardCreateCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if model?.is_buy == "1" {

//                UIColor.gradientColor(view: openButton, colors: [UIColor.lightGray.cgColor,
//                                                                 UIColor.lightGray.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
            openButton.backgroundColor = .lightGray
        } else if model?.is_buy == "2" {
            openButton.backgroundColor = .lightGray
//                UIColor.gradientColor(view: openButton, colors: [UIColor.lightGray.cgColor,
//                                                                 UIColor.lightGray.cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        } else {
            
            UIColor.gradientColor(view: openButton, colors: [UIColor.colorWithHexString(hexString: "#FDC013").cgColor,
                                                             UIColor.colorWithHexString(hexString: "#FA8C08").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        }
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.colorWithHexString(hexString: "#272727")
        
        titleImageView = UIImageView()
        titleImageView.image = UIImage(named: "serviceChoice")
        contentView.addSubview(titleImageView)
        
        leftBgView = UIView()
        leftBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0.1)
        leftBgView.layer.cornerRadius = 14*widthScale
        leftBgView.layer.borderWidth = 1
        leftBgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#454545").cgColor
        contentView.addSubview(leftBgView)
        
        leftTitleBgView = UIImageView()
        leftTitleBgView.image = UIImage(named: "saveMoneyTitleBgSelect")
        leftBgView.addSubview(leftTitleBgView)
        
        leftTitleLabel = HWLabel.createBoldLabel(title: "7 天周卡", color: "#EAD9BD", font: 14*widthScale)
        leftTitleBgView.addSubview(leftTitleLabel)
        
        leftSelectImageView = UIImageView()
        leftSelectImageView.image = UIImage(named: "saveMoneySelect")
        leftBgView.addSubview(leftSelectImageView)
        
        leftContentOne = HWLabel.createBoldLabel(title: "", color: "#EAD9BD", font: 13*widthScale)
        leftBgView.addSubview(leftContentOne)
        
        leftContentTwo = HWLabel.createBoldLabel(title: "", color: "#EAD9BD", font: 13*widthScale)
        leftBgView.addSubview(leftContentTwo)
        
        leftContentThird = HWLabel.createBoldLabel(title: "", color: "#EAD9BD", font: 13*widthScale)
        leftBgView.addSubview(leftContentThird)
        
        leftBgButton = UIButton(type: .custom)
        leftBgButton.addTarget(self, action: #selector(leftBgButtonClick(_:)), for: .touchUpInside)
        leftBgView.addSubview(leftBgButton)
        
        rightBgView = UIView()
        rightBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0)
        rightBgView.layer.cornerRadius = 14*widthScale
        rightBgView.layer.borderWidth = 1
        rightBgView.layer.borderColor = UIColor.colorWithHexString(hexString: "#454545").cgColor
        contentView.addSubview(rightBgView)
        
        rightTitleBgView = UIImageView()
        rightTitleBgView.image = UIImage(named: "saveMoneyTitleBgNormal")
        rightBgView.addSubview(rightTitleBgView)
        
        rightTitleLabel = HWLabel.createBoldLabel(title: "30 天月卡", color: "#EAD9BD", font: 14*widthScale)
        rightTitleBgView.addSubview(rightTitleLabel)
        
        rightSelectImageView = UIImageView()
        rightSelectImageView.image = UIImage(named: "saveMoneySelect")
        rightBgView.addSubview(rightSelectImageView)
        
        rightContentOne = HWLabel.createBoldLabel(title: "", color: "#EAD9BD", font: 13*widthScale)
        rightBgView.addSubview(rightContentOne)
        
        rightContentTwo = HWLabel.createBoldLabel(title: "", color: "#EAD9BD", font: 13*widthScale)
        rightBgView.addSubview(rightContentTwo)
        
        rightContentThird = HWLabel.createBoldLabel(title: "", color: "#EAD9BD", font: 13*widthScale)
        rightBgView.addSubview(rightContentThird)
        
        rightBgButton = UIButton(type: .custom)
        rightBgButton.addTarget(self, action: #selector(rightBgButtonClick(_:)), for: .touchUpInside)
        rightBgView.addSubview(rightBgButton)
        
        openButton = UIButton(type: .custom)
        openButton.layer.cornerRadius = 24*widthScale
        openButton.layer.masksToBounds = true
        openButton.setTitle("开通", for: .normal)
        openButton.setTitleColor(.colorWithHexString(hexString: "#502E05"), for: .normal)
        openButton.titleLabel?.font = .systemFont(ofSize: 16*widthScale, weight: .semibold)
        openButton.frame = CGRect(x: 0, y: 0, width: 339*widthScale, height: 48*widthScale)
        openButton.addTarget(self, action: #selector(openButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(openButton)
//        UIColor.gradientColor(view: openButton, colors: [UIColor.colorWithHexString(hexString: "#FDC013").cgColor,
//                                                         UIColor.colorWithHexString(hexString: "#FA8C08").cgColor], start: CGPoint(x: 0, y: 0), end: CGPoint(x: 1, y: 0))
        
        bottomLabel = HWLabel.createLabel(title: "", color: "#555555", font: 11*widthScale)
        contentView.addSubview(bottomLabel)
        
        rightSelectImageView.isHidden = true
    }
    
    func initFrame() -> Void {
        titleImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(26*widthScale).priority(.high)
            make.width.equalTo(147*widthScale)
            make.top.equalTo(18*widthScale)
        }
        
        leftBgView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(titleImageView.snp.bottom).offset(18*widthScale)
            make.height.equalTo(141*widthScale)
            make.width.equalTo(159*widthScale)
        }
        
        leftTitleBgView.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.width.equalTo(92*widthScale)
            make.height.equalTo(33*widthScale)
        }
        
        leftTitleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        leftSelectImageView.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.top.equalTo(9*widthScale)
            make.width.height.equalTo(14*widthScale)
        }
        
        leftContentOne.snp.makeConstraints { make in
            make.left.equalTo(7*widthScale)
            make.height.equalTo(18*widthScale)
            make.top.equalTo(leftTitleBgView.snp.bottom).offset(18*widthScale)
            make.right.equalTo(-5*widthScale)
        }
        
        leftContentTwo.snp.makeConstraints { make in
            make.left.equalTo(7*widthScale)
            make.right.equalTo(-5*widthScale)
            make.height.equalTo(18*widthScale)
            make.top.equalTo(leftContentOne.snp.bottom).offset(9*widthScale)
        }
        
        leftContentThird.snp.makeConstraints { make in
            make.left.equalTo(7*widthScale)
            make.right.equalTo(-5*widthScale)
            make.height.equalTo(18*widthScale)
            make.top.equalTo(leftContentTwo.snp.bottom).offset(9*widthScale)
        }
        
        leftBgButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        rightBgView.snp.makeConstraints { make in
            make.left.equalTo(leftBgView.snp.right).offset(20*widthScale)
            make.top.equalTo(titleImageView.snp.bottom).offset(18*widthScale)
            make.height.equalTo(141*widthScale)
            make.width.equalTo(159*widthScale)
        }
        
        rightTitleBgView.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.width.equalTo(92*widthScale)
            make.height.equalTo(33*widthScale)
        }
        
        rightTitleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        rightSelectImageView.snp.makeConstraints { make in
            make.right.equalTo(-9*widthScale)
            make.top.equalTo(9*widthScale)
            make.width.height.equalTo(14*widthScale)
        }
        
        rightContentOne.snp.makeConstraints { make in
            make.left.equalTo(2*widthScale)
            make.height.equalTo(18*widthScale)
            make.top.equalTo(rightTitleBgView.snp.bottom).offset(18*widthScale)
            make.right.equalTo(0)
        }
        
        rightContentTwo.snp.makeConstraints { make in
            make.left.equalTo(2*widthScale)
            make.right.equalTo(-5*widthScale)
            make.height.equalTo(18*widthScale)
            make.top.equalTo(rightContentOne.snp.bottom).offset(9*widthScale)
        }
        
        rightContentThird.snp.makeConstraints { make in
            make.left.equalTo(2*widthScale)
            make.right.equalTo(-5*widthScale)
            make.height.equalTo(18*widthScale)
            make.top.equalTo(rightContentTwo.snp.bottom).offset(9*widthScale)
        }
        
        rightBgButton.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        openButton.snp.makeConstraints { make in
            make.top.equalTo(leftBgView.snp.bottom).offset(18*widthScale)
            make.centerX.equalToSuperview()
            make.height.equalTo(48*widthScale)
            make.width.equalTo(339*widthScale)
        }
        
        bottomLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(openButton.snp.bottom).offset(9*widthScale)
            make.height.equalTo(15*widthScale)
            make.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
@objc
extension HWSaveMoneyCardCreateCell {
    func leftBgButtonClick(_ button:UIButton) -> Void {
        leftSelectImageView.isHidden = false
        leftBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0.1)
        
        rightSelectImageView.isHidden = true
        rightBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0)
        
        model?.selectZhou = true
        
        self.selectButtonClick?()
    }
    func rightBgButtonClick(_ button:UIButton) -> Void {
        leftSelectImageView.isHidden = true
        leftBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0)
        
        rightSelectImageView.isHidden = false
        rightBgView.backgroundColor = .colorWithAlphaHexString(hexString: "#D8D8D8", alpha: 0.1)
        
        model?.selectZhou = false
        
        self.selectButtonClick?()
    }
    
    func openButtonClick(_:UIButton) -> Void {
        let actionSheet = UIAlertController(title: "请选择支付方式", message: "", preferredStyle: .actionSheet)
        let action = UIAlertAction(title: "微信", style: .default) { act in
            self.payButtonClick?("2")
        }
        let action1 = UIAlertAction(title: "支付宝", style: .default) { act in
            self.payButtonClick?("3")
        }
        let cancel = UIAlertAction(title: "取消", style: .cancel) { act in
            
        }
        actionSheet.addAction(action)
        actionSheet.addAction(action1)
        actionSheet.addAction(cancel)
        
        HWUniversal.getCurrentVc().present(actionSheet, animated: true)
    }
}
